// proxySubShopPackage.js
const app = getApp()
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopid:'',
    list:[]
  },
  // 修改开关
  onChange(event) {
    var that = this;
    var i = event.currentTarget.dataset.index;
    var isuse = that.data.list[i].isuse == 1 ? 0 : 1;
    that.setData({
      ['list[' + i + '].isuse']: isuse
    })
  },
  // 修改时间
  timeChange:function(event){
    var that = this;
    var i = event.currentTarget.dataset.index;
    var time = event.detail;
    that.setData({
      ['list[' + i + '].time']: time
    })
  },
  // 修改价格
  priceChange:function(event){
    var that = this;
    var i = event.currentTarget.dataset.index;
    var price = event.detail;
    that.setData({
      ['list[' + i + '].price']: price
    })
  },
  // 提交修改
  toUpdate:function(){
    var that = this;
    var list = that.data.list;
    var data = JSON.stringify(list);
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/sumbitShopMenu', data, 'JSON', 'application/json').then((res) => {
      wx.hideLoading();

      if (res.code == 200) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 2000
        })
      } else {
        // wx.showToast({
        //   title: "提交失败",
        //   icon: 'loading',
        //   duration: 2000
        // })
        Dialog.alert({
          title: '',
          message: res.message,
        }).then(() => {});
      }
    }).catch((errMsg) => {
      wx.hideLoading();
      wx.showToast({
        title: errMsg,
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 获取列表
  shopMenu:function(){
    var that = this;
    var data = {id:that.data.shopid};
    app.post('agent/shopMenu', data).then((res) => {
      if (res.code == 200) {
        that.setData({
          list:res.list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: errMsg,
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopid: options.id,
    })
    this.shopMenu();
  }
})